<?php
class Application_Model_BaiViet extends Zend_Db_Table
{
	protected $_name='tintuc';
	
	public function getBaiVietActive($loai_id){
		try{
			$sql = 'Select * From `tintuc` where acti=1 and loaibaiviet_id='.$loai_id;
			$db = $this->_db;
			$db->setFetchMode ( Zend_Db::FETCH_ASSOC );
			$id = $db->fetchAll ( $sql );
			return $id;
		}catch(Exception $ex){
			echo "Can not get tin tuc, Error: ".$ex->getMessage();
			exit;
		}
	}
	
	public function getBaiVietByLoaiId($loaibaiviet_id){
		try{
			$sql = 'Select * From `tintuc` where loaibaiviet_id = '.(int)$loaibaiviet_id ;
			$db = $this->_db;
			$db->setFetchMode ( Zend_Db::FETCH_ASSOC );
			$id = $db->fetchAll ( $sql );
			return $id;
		}catch(Exception $ex){
			echo "Can not get Bai viet by loaibaiviet_id, Error: ".$ex->getMessage();
			exit;
		}
	}
	
	public function getBaiViet($id)
	{
		$id = (int)$id;
		$row = $this->fetchRow('id = ' . $id);
		if (!$row) {
			throw new Exception("Khong tim thay baiviet $id");
		}
		return $row->toArray();
	}//phuong thuc tra ve mot dong theo $id

	public function addBaiViet($data)
	{
		$this->insert($data);
	}//insert

	public function updateBaiViet($data)
	{
		$this->update($data, 'id = '. (int)$data['id']);
	}//update
	
	public function setActive($id,$acti){
		$data=array(
				'acti'=>$acti);
		$this->update($data, (int)$id);
	}

	public function deleteBaiViet($id)
	{
		$this->delete('id =' . (int)$id);
	}//drop
	
}